const { app, BrowserWindow, Menu, globalShortcut } = require("electron");
const { resolve, dirname } = require("path");

const Store = require("electron-store");
const mkdirp = require("mkdirp");
const path = require("path");
const store = new Store();
const menu = Menu.buildFromTemplate([]);
// let { parseId } = require("./src/utils/index");
Menu.setApplicationMenu(menu);
var exePath = dirname(app.getPath("exe"));
let workdirPath = resolve(exePath, "workdir");
mkdirp.sync(workdirPath);
store.set("workdir", workdirPath);

console.log(workdirPath);

// console.log("aaaa", parseId("a6Kf6ohiYcNvwwdL6NMmlDTnv0Rhr2x1y7GZJbC1FvYxXl16RoaM8kaTxQhdA9Ma").toString());

//获取单例锁
const gotTheLock = app.requestSingleInstanceLock();
// 自定义菜单

if (!gotTheLock) {
  app.quit();
} else {
  let mainWindow;
  async function createWindow() {
    const win = new BrowserWindow({
      width: 800,
      height: 700,
      // resizable: false,
      //useContentSize: true,
      /* transparent: true, */
      icon: path.join(__dirname, "src/favicon.ico"),
      // frame: false,
      show: false,
      webPreferences: {
        enableRemoteModule: false,
        webSecurity: false,
        nodeIntegration: true,
        contextIsolation: true,
        webviewTag: true,
        preload: path.join(__dirname, "src/preload.js"),
      },
    });

    /* win.maximize(); */
    // if (config.ENV == "local") {
    //   await win.loadURL("http://127.0.0.1:5174/");
    // } else {
    //   await win.loadFile(path.join(__dirname, "view/index.html"));
    // }
    
    require("./src/event")(win);
    await win.loadFile(path.join(__dirname, "src/index.html"));

    // win.webContents.openDevTools();

    globalShortcut.register("Alt+f", function () {
      win.webContents.openDevTools();
    });

    win.on("closed", () => {
      mainWindow = null;
    });
    win.on("ready-to-show", () => {
      win.show();
    });

    return win;
  }

  app.on("ready", async function () {
    mainWindow = await createWindow();
  });
  app.on("second-instance", () => {
    if (mainWindow) {
      console.log(mainWindow);
      if (mainWindow.isMinimized()) mainWindow.restore();
      mainWindow.focus();
    }
  });

  app.on("quit", () => {
    app.releaseSingleInstanceLock(); //释放所有的单例锁
  });
}
